package com.whcoding.test.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @author Manager
 */
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils{


    private static final Logger LOGGER = LoggerFactory.getLogger(CollectionUtils.class);

    private static final int DEFAULT_SIZE = 1000;

    /**
     *
     * <p>拆分List为固定大小的多个集合</p>
     * <p>推荐使用</p>
     * <p>返回集合的size越小,此方法性能越高</p>
     * @param baseList
     * @param size
     * @return ArrayList
     * @autho makejava
     * @time 2019-4-12
     */
    @SuppressWarnings("unchecked")
    public static <T> List<List<T>> fastSplitList(List<T> baseList, int size) {
        if (baseList == null || baseList.size() == 0) {
            return null;
        }
        if (size <= 0) {
            size = DEFAULT_SIZE;
        }
        int arrSize = baseList.size() % size == 0 ? baseList.size() / size : baseList.size() / size + 1;
        List<List<T>> resultList = new ArrayList<List<T>>();
        for (int i = 0; i < arrSize; i++) {
            if (arrSize - 1 == i) {
                resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, baseList.size())));
            } else {
                resultList.add((List<T>) new ArrayList<Object>( baseList.subList(i * size, size * (i + 1))));
            }
        }
        return resultList;
    }

    /**
     *
     * <p>拆分List为固定大小的多个集合</p>
     * <p>返回集合的size越大,此方法性能越高</p>
     * @param baseList
     * @param size
     * @return ArrayList
     * @autho makejava
     * @time 2019-4-12
     */
    public static <T> List<List<T>> splitList(List<T> baseList, int size) {
        if (baseList == null || baseList.size() == 0) {
            return null;
        }
        if (size <= 0) {
            size = DEFAULT_SIZE;
        }
        List<List<T>> resultList = new ArrayList<List<T>>();
        for (int i = 0; i < baseList.size(); ++i) {
            if (i % size == 0) {
                List<T> result = new ArrayList<T>();
                resultList.add(result);
            }
            resultList.get(i / size).add(baseList.get(i));
        }
        return resultList;
    }

    /**
     *
     * <p>集合转Set</p>
     * @param coll 源集合
     * @param keyType 属性类型
     * @param keyMethodName 属性get方法
     * @return LinkedHashSet
     * @autho makejava
     * @time 2019-4-12
     */
    public static <K, V> Set<K> asSet(final Collection<V> coll, final Class<K> keyType
            , final String keyMethodName) {
        if (CollectionUtils.isEmpty(coll)) {
            return new HashSet<K>(0);
        }
        final Set<K> set = new LinkedHashSet<K>(coll.size());
        try {
            for (final V value : coll) {
                Object object;
                Method method = value.getClass().getMethod(keyMethodName);
                object = method.invoke(value);
                @SuppressWarnings("unchecked")
                final K key = (K) object;
                set.add(key);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new CollectionUtilsException("Collection conversion Set exceptions");
        }
        return set;
    }

    /**
     *
     * <p>集合转Map</p>
     * <p>比如:List<EmployeeEntity>,讲EmployeeEntity的name属性作为key,转换成Map</p>
     * @param coll 源集合
     * @param keyType 属性类型
     * @param valueType 源数据类型(实体类型)
     * @param keyMethodName 属性get方法
     * @return LinkedHashMap
     * @autho makejava
     * @time 2019-4-12
     */
    public static <K, V> Map<K, V> asMap(final Collection<V> coll,final Class<K> keyType
            ,final Class<V> valueType,final String keyMethodName) {
        if (CollectionUtils.isEmpty(coll)) {
            return new LinkedHashMap<K, V>(0);
        }
        final Map<K, V> map = new LinkedHashMap<K, V>(coll.size());
        try {
            Method method = valueType.getMethod(keyMethodName);
            for (final V value : coll) {
                Object object;
                object = method.invoke(value);
                @SuppressWarnings("unchecked")
                final K key = (K) object;
                map.put(key, value);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new CollectionUtilsException("Collection conversion Map exceptions");
        }
        return map;
    }

    /**
     *
     * <p>集合转List</p>
     * @param coll
     * @return ArrayList
     * @autho makejava
     * @time 2019-4-12
     */
    public static <V> List<V> asList(final Collection<V> coll) {
        if (CollectionUtils.isEmpty(coll)) {
            return new ArrayList<V>(0);
        }
        final List<V> list = new ArrayList<V>();
        for (final V value : coll) {
            if (value != null) {
                list.add(value);
            }
        }
        return list;
    }


    /**
     * <p>集合<String>toString</p>
     *
     * @param collection 泛型必须为String类型
     * @param split      比如连接符","
     * @return
     * @autho makejava
     * @time 2019-4-12
     */
    public static String collToString(Collection<String> collection, String split) {
        StringBuilder sb = new StringBuilder();
        if (collection != null) {
            int i = 0, size = collection.size();
            for (Iterator<String> iterator = collection.iterator(); iterator.hasNext(); ) {
                String str = iterator.next();
                sb.append(str);
                if (++i < size) {
                    sb.append(split);
                }
            }
        }
        return sb.toString();
    }




    static class CollectionUtilsException extends RuntimeException{

        private static final long serialVersionUID = 1L;

        public CollectionUtilsException(String s) {
            super(s);
        }

        public CollectionUtilsException(String s, Throwable e) {
            super(s, e);
        }

        public CollectionUtilsException(Throwable e) {
            super(e);
        }
    }


}
